<?php

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Capsule\Manager as DB;

class cartModel extends Model
{

    protected $table = 'cart';
    protected $primaryKey = 'id';
    protected $appends = [
    ];


    public function getGoodsComboAttribute($value)
    {
        return $value ? json_decode($value) : null;
    }

    public static function info()
    {
        $userinfo = \app\service\auth::userinfo();
        $user = memberModel::find($userinfo['id']);

        // 被物理删除的商品直接删掉购物车，只删除自己的
        DB::table('cart')
            ->leftJoin('goods', 'cart.goods_id', '=', 'goods.id')
            ->where('cart.member_id', '=', $user->id)
            ->where(function ($query) {
               $query->where('goods.status', '=', 'down')
                     ->orWhere('goods.id', null);
            })
            ->delete();

        $list = self::with(['goods', 'skuPrice'])->where('member_id', $user->id)->get()->toArray();
        foreach ($list as $k => $v) {
            $sku_price = $v['sku_price'];
            $origin_goods_sku_text = implode(',', $sku_price['goods_sku_text']);
            $list[$k]['sku_price']['goods_sku_text'] = $origin_goods_sku_text;
        }

        return $list;
    }

    public static function add($goodsList)
    {
        $userinfo = \app\service\auth::userinfo();
        $user = memberModel::find($userinfo['id']);

        foreach ($goodsList as $v) {
            $where = [
                'member_id'    => $user->id,
                'goods_id'     => $v['goods_id'],
                'sku_price_id' => $v['sku_price_id'],
            ];
            $cart = self::where($where)->first();
            if ($cart) {
                $cart->goods_num += $v['goods_num'];
                $cart->updated_at = timestamp();
                $cart->save();
            } else {
                $cartData = [
                    'member_id'    => $user->id,
                    'goods_id'     => $v['goods_id'],
                    'goods_num'    => $v['goods_num'],
                    'sku_price_id' => $v['sku_price_id'],
                    'created_at'   => timestamp(),
                ];
                // if (isset($v['goods_combo']) && $v['goods_combo']) {
                //     $cartData['goods_combo'] = json_encode($v['goods_combo']);
                // }
                $cart = self::insert($cartData);
            }
        }

        return $cart;
    }

    public static function edit($params)
    {
        $userinfo = \app\service\auth::userinfo();
        $user = memberModel::find($userinfo['id']);

        $act = $params['act'];
        $cart_list = $params['cart_list'];
        $value = $params['value'];

        $where['user_id'] = $user->id;
        switch ($act) {
            case 'delete':
                foreach ($cart_list as $v) {
                    self::where('id', $v)->delete();
                }
                break;
            case 'change':
                foreach ($cart_list as $v) {
                    self::where('id', $v)->update(['goods_num' => $value, 'updated_at' => timestamp()]);
                }
                break;
        }

        return true;
    }

    public function goods()
    {
        return $this->hasOne(goodsModel::class, 'id', 'goods_id');
    }

    public function skuPrice()
    {
        return $this->hasOne(goodsskupriceModel::class, 'id', 'sku_price_id');
    }

}
